package com.app.bimo.commonbusiness.network;

import com.app.bimo.commonbusiness.BuildConfig;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observer;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

public class DownRetrofitUtil {
    private Retrofit mRetrofit;
    public DownRetrofitUtil(String baseUrl, Observer<Integer> observer) {
        OkHttpClient okHttpClient;
        DownloadInterceptor mInterceptor = new DownloadInterceptor(observer);
        if(BuildConfig.DEBUG){
            HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
            logging.setLevel(HttpLoggingInterceptor.Level.BODY);
            okHttpClient = UnsafeOkHttpClient.getUnsafeOkHttpClient().newBuilder()
                    .retryOnConnectionFailure(true)
                    .addNetworkInterceptor(mInterceptor)
                    .connectTimeout(5, TimeUnit.MINUTES)
                    .addInterceptor(logging)
                    .build();
        }else{
            okHttpClient = UnsafeOkHttpClient.getUnsafeOkHttpClient().newBuilder()
                    .retryOnConnectionFailure(true)
                    .connectTimeout(5, TimeUnit.MINUTES)
                    .addNetworkInterceptor(mInterceptor)
                    .build();
        }
        mRetrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(okHttpClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(CustomGsonConverterFactory.create())
                .build();
    }
    public <T> T  createService(Class<T> serviceClass){
        return mRetrofit.create(serviceClass);
    }
}
